package com.huang.dao;

import com.huang.pojo.User;
import com.huang.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaotext {
    @Test
    public void getUserlike(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userlike = mapper.getUserlike("%小%");
        for (User user :userlike) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    @Test
    public void text(){
        //第一步获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try{
            //方法一
            //第二步执行SQL语句
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = mapper.getUserList();
            //方法二
//淘汰了//List<User> userList=sqlSession.selectList("com.huang.dao.UserDao.getUserList");
            for (User user:userList) {
                System.out.println(user);
            }
        }finally {
            sqlSession.close();
        }
    }
    @Test
    public void getUserid(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserid(2);
        System.out.println(user);
        sqlSession.close();
    }
    @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(4);
        //提交事务******
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void insertUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
//        User user=new User(5,"猪猪","147852");
        mapper.insertUser(new User(3,"猪猪","147852"));
        //提交事务******
        sqlSession.commit();
        //关闭sqlSession
        sqlSession.close();
    }
    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(3,"小白","369852"));
        //提交事务******
        sqlSession.commit();
        //关闭sqlSession
        sqlSession.close();
    }
//    **************************Map应用
    @Test
    public void insertUser2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        Map<String,Object> map=new HashMap<>();
        map.put("userid",4);
        map.put("username","朱科");
        map.put("userpwd","000000");
        mapper.insertUser2(map);
        sqlSession.commit();
        sqlSession.close();
    }
}
